package com.chinar.funds;

import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.chinar.funds.until.FundDatabaseHelper;

public class RegisterActivity extends AppCompatActivity {
private static final String TAG = "RegisterActivity";
private EditText et_register_username;
private EditText et_register_nickname;
private EditText et_register_password;
private EditText et_register_confirm_password;
private Button btn_register_register;
private Button btn_register_back;
private FundDatabaseHelper dbHelper;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();

        setContentView(R.layout.activity_register);
        initViews();
        dbHelper=new FundDatabaseHelper(this);
        btn_register_register.setOnClickListener(v ->adduser());
        btn_register_back.setOnClickListener(v -> finish());
    }
    private void initViews() {
        et_register_username = findViewById(R.id.register_et_username);
        et_register_nickname = findViewById(R.id.register_et_nickname);
        btn_register_register = findViewById(R.id.btn_register_register);
        et_register_password = findViewById(R.id.register_et_pwd);
        et_register_confirm_password = findViewById(R.id.register_et_pwd2);
        btn_register_back=findViewById(R.id.btn_register_back);

    }

    private void adduser() {
        String username = et_register_username.getText().toString();
        String nickname = et_register_nickname.getText().toString();
        String password = et_register_password.getText().toString();
        String confirm_password = et_register_confirm_password.getText().toString();
        Log.d(TAG, "adduser开始: " + username + " " + nickname + " " + password + " " + confirm_password);
        if (username.isEmpty() || nickname.isEmpty() || password.isEmpty() || confirm_password.isEmpty()) {
            Toast.makeText(this, "请填写完整信息", Toast.LENGTH_SHORT).show();
        } else if (!password.equals(confirm_password)) {
            Toast.makeText(this, "密码不一致", Toast.LENGTH_SHORT).show();
        } else if(username.length()!=11 || !username.startsWith("1")) {
            Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
        } else if(nickname.length()>4) {
            Toast.makeText(this, "昵称不能超过四个字", Toast.LENGTH_SHORT).show();
        } else if(password.length() < 6) {
            Toast.makeText(this, "密码长度不能少于6位", Toast.LENGTH_SHORT).show();
        } else if(!isValidPassword(password)) {
            Toast.makeText(this, "密码只能包含数字和字母", Toast.LENGTH_SHORT).show();
        } else {
            // 注意：在insertUser方法中，密码会被自动加密存储
            if (dbHelper.insertUser(username, password, nickname)) {
                Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
                finish(); // 注册成功后返回登录页面
            } else {
                Toast.makeText(this, "用户已存在", Toast.LENGTH_SHORT).show();
            }
        }
    }
    
    /**
     * 验证密码是否只包含数字和字母
     * @param password 密码
     * @return 是否有效
     */
    private boolean isValidPassword(String password) {
        return password.matches("^[0-9a-zA-Z]+$");
    }
}